<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <div class="" v-show="bianliang">
      <van-nav-bar class="page-nav-bar" title="账号登录">
        <template #left>
          <!-- 后退上一页:  $router.back() 或 $router.go(-1) -->
          <van-icon name="cross" @click="$router.go(-1)" />
        </template>
      </van-nav-bar>
      <!-- /导航栏 -->

      <!-- 登录表单 -->

      <van-form ref="loginForm" @submit="onSbmit">
        <van-field
          class="du"
          name="mobile"
          placeholder="请输入账号"
          :rules="userFormRules.username"
          type="String"
          maxlength="11"
          v-model="user.username"
        >
          <template #left-icon>
            <i class="toutiao toutiao-shouji"></i>
          </template>
        </van-field>
        <van-field
          class="du"
          name="code"
          v-model="user.password"
          :rules="userFormRules.password"
          placeholder="请输入密码"
        >
        </van-field>
        <div class="login-btn-wrap">
          <van-button class="login-btn" block type="info" native-type="submit">
            登录
          </van-button>
          <p @click="xianshi">还没有账号，立即注册</p>
        </div>
      </van-form>
    </div>
    <!-- /登录表单 -->

    <!-- ####################################### -->

    <!-- 注册 -->

    <div class="" v-show="zhuce">
      <van-nav-bar class="page-nav-bar" title="注册">
        <template #left>
          <!-- 后退上一页:  $router.back() 或 $router.go(-1) -->
          <van-icon name="cross" @click="$router.back()" />
        </template>
      </van-nav-bar>
      <!-- /导航栏 -->

      <!-- 登录表单 -->

      <van-form ref="loginForm" @submit="onZhuce">
        <van-field
          class="du"
          name="mobile"
          placeholder="请输入账号"
          maxlength="11"
          v-model="zhuce1.username"
          :rules="userFormRules.password"

        >
          <template #left-icon>
            <i class="toutiao toutiao-shouji"></i>
          </template>
        </van-field>
        <van-field
          class="du"
          name="code"
          v-model="mima"
          placeholder="请输入密码"
          :rules="userFormRules.password"
        >
        </van-field>
        <van-field
          class="du"
          name="code"
          v-model="zhuce1.password"
          placeholder="请重新输入密码"
          :rules="userFormRules.chongfu"
        >
        </van-field>

        <div class="login-btn-wrap">
          <van-button class="login-btn" block type="info" native-type="submit">
            注册
          </van-button>
          <p class="zuo" @click="$router.push('/')">点我返回首页</p>
          <p class="you" @click="q">已有账号，去登陆</p>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
// import { login, sendSms } from '@/api/user'
import { userLogin, userReg } from '@/api/user'

export default {
  name: 'LoginIndex',
  data () {
    const chongfuValidator = val => {
      return this.mima === val
    }
    return {
      // fromData: [],
      username: '',
      password: '',
      yonghuming: '',
      mima: '',
      querenmima: '',
      bianliang: true,
      zhuce: false,
      user: {
        username: 'ccccc',
        password: '111111'
      },
      zhuce1: {
        username: '',
        password: ''
      },
      userFormRules: {
        username: [{ required: true, message: '请填写用户名' }, { pattern: /^.{2,8}$/, message: '最小2位最大8位' }],
        password: [{ required: true, message: '请填写用户名' }],
        chongfu: [{ required: true, message: '请填写用户名' }, { validator: chongfuValidator, message: '两次密码不一致' }]
      }
    }
  },
  methods: {
    xianshi () {
      console.log(111)
      this.bianliang = false
      // 控制显示
      this.zhuce = true
    },
    q () {
      this.bianliang = true
      // 控制显示
      this.zhuce = false
    },
    // 登录接口
    async onSbmit () {
      const user = this.user

      this.$toast.loading({
        message: '登录中',
        forbidClick: true,
        duration: 0
      })

      try {
        const { body,status } = await userLogin(user)
        this.$store.commit('setUser', body)
        // console.log(res)
        
        if (status === 400) {
          this.$toast.fail('账号或密码错误')
        }else{
          this.$toast.success('登录成功')
        // this.$router.push('/profile')
        this.$router.back()
        }
      } catch (error) {
        console.dir(error)
        // if (error.status === 400) {
        //   this.$toast.fail('账号或密码错误')
        // } else {
        //   this.$toast.fail('登录失败，请稍后重试')
        // }
      }
    },
    // 注册
    async onZhuce () {
      const s = this.zhuce1
      try {
        const res = await userReg(s)
        if (res.status === 200) {
          this.$store.commit('setUser', res.body)
          this.$toast.success('创建成功')
        } else if (res.status === 400) {
          this.$toast.fail(res.description)
        } else {
          this.$toast.fail('请稍后重试')
        }
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="less">
.zuo {
  float: left;
}
.you {
  float: right;
}
p {
  margin-top: 30px;
  text-align: center;
  font-size: 12px;
}
.page-nav-bar {
  margin: 40px;
  font-size: 40px;
}
.du {
  margin-top: 10px;
  height: 75px;
}
.login-container {
  .toutiao {
    font-size: 37px;
  }

  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
    padding: 0;
  }

  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #1cb676;
      border: none;
      height: 90px;
    }
  }
  .van-cell{
    padding: 0.5rem 0.5rem 1.2rem;
  }
}
</style>
